<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <!-- <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" /> -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <script type="text/javascript" src="../../js/jquery-3.2.1.js"></script>
    <script type="text/javascript" src="../../lib/layui/layui.js" charset="utf-8"></script>
  </head>
  
  </head>
  
  <body>
    <div class="x-body" id="updatebox" data-id="">
        <form action="" method="post" class="layui-form layui-form-pane" lay-filter="Textform">
                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>姓名
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="officemanName" name="officemanName" required="" lay-verify="required"
                        autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>年龄
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="officemanAge" name="officemanAge" required="" lay-verify="required"
                        autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item" style="width:300px;">
					<label for="name" class="layui-form-label"> <span
						class="x-red">*</span>性别</label>
					<div class="layui-input-block" id = "view">
						<select id="officemanSex" name="officemanSex" lay-filter="choosetype" 
						required="" lay-verify="required" autocomplete="off">
						<option value="男" >男</option>
						<option value="女" >女</option>
		  				</select>
					</div>
				</div>
				<div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span></span>联系方式
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="officemanTelephone" name="officemanTelephone"
                        autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>职业
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="job" name="job" required="" lay-verify="required"
                        autocomplete="off" class="layui-input">
                    </div>
                </div>
                
                <div class="layui-form-item" style="width:300px;">
					<label class="layui-form-label"> <span
						class="x-red">*</span>所在办公室</label></label>
					<div class="layui-input-block" id = "view">
						<select id="blgOfficeId" name="blgOfficeId" lay-filter="choosevillage" required="" lay-verify="required">
						
		  				</select>
					</div>
				</div>
				<div class="layui-form-item" style="width:300px;">
					<label class="layui-form-label">上司</label>
					<div class="layui-input-block" id = "view">
						<select id="blgSuperId" name="blgSuperId">
						<option value="0" >请选择</option>
		
		  				</select>
					</div>
				</div>
                
                <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="add">修改</button>
              </div>
            </form>
    </div>
    <script>
 
    

        layui.use(['form','layer'], function(){
          var form = layui.form
          ,layer = layui.layer;
          
          //从url中截取传入的参数
          //这种方法获得的所有参数是一个json的值
          function GetRequest() {
        	   var url = location.search; //获取url中"?"符后的字串
        	   var theRequest = new Object();
        	   if (url.indexOf("?") != -1) {
        	      var str = url.substr(1);
        	      strs = str.split("&");
        	      for(var i = 0; i < strs.length; i ++) {
        	         theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
        	      }
        	   }
        	   return theRequest;
          }
          //从json中获取对应的参数值
          var id =GetRequest().id;
          //在主块中绑定一个这条信息的ID
          $("#updatebox").attr("data-id",id);
          
          
          $(function(){
         	 
        	  $.ajax({
        		  type:"post",
        		  url:"../../office/showoffice",
        		  async:false,
				  success:function(result){
					  $.each(result.listoffice,function(index,value){
	              			//下列2种方法其中一种都可以
	              		    $("#blgOfficeId").append("<option value="+value.officeAreaId+">"+value.officeName+"</option>");
	              		    form.render();
	              		});
				  },
				  error:function(){
					  layer.alert("办公室信息的下拉菜单没有被正确地加载!", {icon: 2,time:1500});
				  }
        	  });
        	  
        	  
        	  $.ajax({
        		  type:"post",
        		  url:"../../office/showofficeman",
        		  async:false,
				  success:function(result){
					  $.each(result.listOfficeman,function(index,value){
	              			//下列2种方法其中一种都可以
	              		    $("#blgSuperId").append("<option value="+value.officemanId+">"+value.officemanName+"---"+value.job+"</option>");
	              		    form.render();
	              		});
				  },
				  error:function(){
					  layer.alert("选择上司的下拉菜单没有被正确地加载!", {icon: 2,time:1500});
				  }
        	  });
        	  
        	  
        	  $.ajax({
              	type: "post",
                	url: "../../office/showOfficemanByKey",
                	async: true,
                	data:{
                		"id":id
                	},
                	success: function(result){
                		var json={};
                      json.officemanName=result.officeman.officemanName;
                      json.officemanAge=result.officeman.officemanAge;
                      json.officemanSex=result.officeman.officemanSex;
                      json.officemanTelephone=result.officeman.officemanTelephone;
                      json.job=result.officeman.job;
                      json.blgOfficeId=result.officeman.blgOfficeId;
                      json.blgSuperId=result.officeman.blgSuperId;
                      //表单值初始化将查询到的值放在表单中
                      form.val("Textform",json); 
                      
                      $("#blgOfficeId").val(result.officeman.blgOfficeId);
                      $("#blgSuperId").val(result.officeman.blgSuperId);
                	},
                	error: function(){
                		layer.alert("表单初始化失败!", {icon: 2,time:1500});
                	}
                });
        	  
          });
          
          //监听提交
          form.on('submit(add)', function(data){
        	 
        	var id = document.getElementById("updatebox").getAttribute("data-id");
        	var json={};
            json.officemanId=id;
            json.officemanName=document.getElementById("officemanName").value;
            json.officemanAge=document.getElementById("officemanAge").value;
            json.officemanSex=document.getElementById("officemanSex").value;
            json.officemanTelephone=document.getElementById("officemanTelephone").value;
            json.job=document.getElementById("job").value;
            json.blgOfficeId=document.getElementById("blgOfficeId").value;
            json.blgSuperId=document.getElementById("blgSuperId").value;
           
            
            $.ajax({
            	type: "post",
            	url: "../../office/updateofficeman",
            	async: true,
            	data:json,
            	dataType:'json',
            	success: function(result) {
            		if(result.msg == 'success') {
              			layer.msg("修改成功",{icon:6,time:1500},function(){
              				var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
              				parent.location.reload();
              				parent.layer.close(index); //再执行关闭 
                  		});
            		} else {
            			layer.msg("修改失败",{icon:5,time:1500},function(){
                			
              			});
            		} 
            	},   	
            	error: function() {
                    layer.alert("数据异常", {icon: 2,time:1500});
                }
            });
            return false;
          });
          
        });
          
          
   </script>

  </body>

</html>